// ZhuangQiuGame.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<graphics.h>
#include<conio.h>
#define width 640
#define height 480
#define PI 3.14
float ball_x, ball_y;
int ball_vx, ball_vy;
int radius=20;
int bar_x = width / 2;
int bar_left, bar_right, bar_top, bar_bottom;

void initData();
void show();
void autoAnimate();
void clean();
void animateControl();
int main()
{
	initgraph(width, height);
	initData();
	BeginBatchDraw();
	while (1)
	{
		clean();
		fillcircle(ball_x, ball_y, radius);
		fillrectangle(bar_left, bar_top, bar_right, bar_bottom);
		
		
		autoAnimate();
		
        bar_left = bar_x - 70;
		bar_right = bar_x + 70;
		animateControl();
		autoAnimate();
		show();
		Sleep(4);
		FlushBatchDraw();
	}
	EndBatchDraw();
	_getch();
	closegraph();
    return 0;
}
void initData()
{
	ball_x = width / 2;
	ball_y = height / 2;
	ball_vx = 1;
	ball_vy = 1;
	radius = 20;
	bar_left = bar_x - 100;
	bar_right = bar_x + 100;
	bar_top = 460;
	bar_bottom = 480;
}
void show()
{
	setfillcolor(BLUE);
	fillcircle(ball_x, ball_y, radius);
	//画挡板
	setfillcolor(RED);	
	fillrectangle(bar_left, bar_top, bar_right, bar_bottom);
}
void animateControl()
{
	char inputC;
	if (_kbhit())
	{
		inputC = _getch();
		switch (inputC)
		{
		case 'a':
			bar_x -= 10;
			break;
		case 'd':
			bar_x += 10;
			break;
		default:
			break;
		}
	}
}
void clean()
{
	setcolor(BLACK);
	setfillcolor(BLACK);
}
void autoAnimate()
{
	ball_x = ball_x + ball_vx;
	ball_y = ball_y + ball_vy;
	if (ball_x <= radius || ball_x >= width - radius)
	{
		ball_vx = -ball_vx;
	}
	if (ball_y <= radius || ball_y >= height - radius)
	{
		ball_vy=-ball_vy;
	}
	if (ball_x >= bar_left&&ball_x <= bar_right&&ball_y + 30 + radius >= height)
	{
		ball_vy = -ball_vy;
		ball_vx = -ball_vx;
	}
}
